<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Node.js服务器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 5px;
            line-height: 1.6;
        }

        h1 {
            color: #333;
            text-align: center;
        }

        .container {
            background-color: #f9f9f9;
            border-radius: 8px;
            padding: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 5px 15px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }

        .result {
            margin-top: 10px;
            padding: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            min-height: 40px;
        }

        input[type="text"],
        input[type="url"],
        textarea {
            width: 100%;
            padding: 10px;
            margin: 8px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        textarea {
            height: 50px;
            resize: vertical;
        }

        label {
            font-weight: bold;
        }

        .pdf-links {
            margin-top: 15px;
        }

        .pdf-links a {
            display: inline-block;
            margin-right: 15px;
            text-decoration: none;
            color: #0066cc;
        }

        .pdf-links a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2 style="text-align: center;">PDF生成工具</h2>

        <div>
            <h3>1.从URL生成PDF</h3>
            <form id="urlPdfForm">
                <div>
                    <label for="pdfUrl">输入网页URL：</label>
                    <input type="url" id="pdfUrl" name="url" placeholder="https://example.com" required>
                </div>
                <div>
                    <label for="urlFilename">PDF文件名：</label>
                    <input type="text" id="urlFilename" name="filename" placeholder="output.pdf" required>
                </div>
                <button type="submit">生成PDF</button>
            </form>
            <div id="urlPdfResult" class="result"></div>
        </div>

        <div class="pdf-links" id="pdfLinks">
            <h2>生成的PDF文件</h2>
            <p>生成的PDF将显示在这里...</p>
        </div>
    </div>

    <script>
        // 从URL生成PDF
        document.getElementById('urlPdfForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const resultDiv = document.getElementById('urlPdfResult');
            resultDiv.textContent = '正在生成PDF，请稍候...';

            const url = document.getElementById('pdfUrl').value;
            const filename = document.getElementById('urlFilename').value;

            try {
                const response = await fetch('/api/url-to-pdf', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ url, filename })
                });

                const data = await response.json();
                if (data.success) {
                    resultDiv.textContent = `PDF生成成功！`;
                    addPdfLink(data.downloadUrl, filename);
                } else {
                    resultDiv.textContent = `错误: ${data.error}`;
                }
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
            }
        });

        // 添加PDF文件链接
        function addPdfLink(url, filename) {
            const linksDiv = document.getElementById('pdfLinks');

            // 移除提示文本
            if (linksDiv.querySelector('p')) {
                linksDiv.removeChild(linksDiv.querySelector('p'));
            }

            const link = document.createElement('a');
            link.href = url;
            link.textContent = filename;
            link.target = '_blank';

            const linkWrapper = document.createElement('div');
            linkWrapper.appendChild(link);
            linksDiv.appendChild(linkWrapper);
        }
    </script>
</body>

</html>